/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modbus;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leandro
 */
public class DriverTCP extends Driver {

   private Socket socketTcp;
   

    public boolean concectTcp(String ipAddres,int port,int time, int reint){
        this.timeout = time;
        this.nroReintentos = reint;
        boolean conn=true;
        this.connected=true;
        try {
            socketTcp = new Socket(ipAddres, port);
            inputStream = socketTcp.getInputStream();
            outputStream = socketTcp.getOutputStream();

        } catch (UnknownHostException ex) {
            Logger.getLogger(DriverTCP.class.getName()).log(Level.SEVERE, null, ex);
            SuscriptorMensajes.getI().notificar("Host de destino desconocido");
            conn=false;
            this.connected=false;
        } catch (IOException ex) {
            Logger.getLogger(DriverTCP.class.getName()).log(Level.SEVERE, null, ex);
            SuscriptorMensajes.getI().notificar("Excepción de IO");
            conn=false;
            this.connected=false;
        }

        return conn;
    }

    public boolean disconnectTcp() {
        boolean disconn = true;
//		end = true;

        try {
            outputStream.close();
            inputStream.close();
            outputStream.flush();
            socketTcp.close();
        } catch (IOException e) {
            e.printStackTrace();
            SuscriptorMensajes.getI().notificar("Excepcion al tratar de desconectar");
            disconn = false;
        }
        
        this.connected = false;

        System.out.println("Desconectado");
        SuscriptorMensajes.getI().notificar("Desconectado");
        return disconn;
    }

    @Override
    public boolean send(Paquete p) {
        boolean success = false;
        if (isConnected()) {
            try {
                outputStream.write(p.getPaqueteTCP());
                success = true;
            } catch (IOException e) {
                SuscriptorMensajes.getI().notificar("No se pudo enviar");
                disconnect();
            }
        } else {
            System.out.println("No hay un socket conectado");
            SuscriptorMensajes.getI().notificar("No esta conectado el driver");
        }
        return success;
    }

    @Override
    public byte[] receive() {
        int cantInt = 0;
        boolean recibi = false;
        byte[] buffer = new byte[1024];
        int len = -1, i, temp;
        InputStream in = this.getInputStream();
        byte[] aux = null;
        byte[] aux2;
        int[] crcCalc;
        if (isConnected()) {
            try {
                if ((in.available()) > 0) {
                    if ((len = in.read(buffer)) > -1) {
                        recibi = true;
                        aux = new byte[len];
                        for (i = 0; i < len; i++) {
                            //Código de tratamiento de la informacion
                            aux[i] = buffer[i];

                        }
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(ExpertoComunicacion.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("no pude leer");
                SuscriptorMensajes.getI().notificar("Excepción al tratar de leer");
            }
        } else {
            SuscriptorMensajes.getI().notificar("Driver no conectado");
        }

        return aux;
    }


    public boolean disconnect() {
        boolean disconn = true;
//		end = true;

        try {
            outputStream.close();
            inputStream.close();
            outputStream.flush();
            socketTcp.close();
            connected = false;
            System.out.println("Desconectado");
            SuscriptorMensajes.getI().notificar("Desconectado");

        } catch (IOException e) {
            e.printStackTrace();
            disconn = false;
            SuscriptorMensajes.getI().notificar("Excepción al descoectar");
        }

        
        

        
        return disconn;
    }
}
